package com.uls.web.controller.found;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.uls.common.core.domain.entity.SysUser;
import com.uls.found.domain.EduClaimRecord;
import com.uls.framework.config.MinioProp;
import com.uls.framework.utils.MinioUtils;
import com.uls.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.found.domain.EduLostFoundNotice;
import com.uls.found.service.IEduLostFoundNoticeService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 寻物启事Controller
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/found/notice")
public class EduLostFoundNoticeController extends BaseController
{
    @Autowired
    private IEduLostFoundNoticeService eduLostFoundNoticeService;

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private MinioProp minioProp;

    /**
     * 查询寻物启事列表
     */
    @PreAuthorize("@ss.hasPermi('found:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(EduLostFoundNotice eduLostFoundNotice)
    {
        startPage();
        List<EduLostFoundNotice> list = eduLostFoundNoticeService.selectEduLostFoundNoticeList(eduLostFoundNotice);

        for (EduLostFoundNotice eduLostFoundNotice1:list){

            SysUser sysUser= iSysUserService.selectUserById(eduLostFoundNotice1.getStudentId());

            eduLostFoundNotice1.setUserName(sysUser.getUserName());
        }
        return getDataTable(list);
    }

    /**
     * 导出寻物启事列表
     */
    @PreAuthorize("@ss.hasPermi('found:notice:export')")
    @Log(title = "寻物启事", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EduLostFoundNotice eduLostFoundNotice)
    {
        List<EduLostFoundNotice> list = eduLostFoundNoticeService.selectEduLostFoundNoticeList(eduLostFoundNotice);
        ExcelUtil<EduLostFoundNotice> util = new ExcelUtil<EduLostFoundNotice>(EduLostFoundNotice.class);
        util.exportExcel(response, list, "寻物启事数据");
    }

    /**
     * 获取寻物启事详细信息
     */
    @PreAuthorize("@ss.hasPermi('found:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") Integer noticeId)
    {
        return success(eduLostFoundNoticeService.selectEduLostFoundNoticeByNoticeId(noticeId));
    }

    /**
     * 新增寻物启事
     */
    @PreAuthorize("@ss.hasPermi('found:notice:add')")
    @Log(title = "寻物启事", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduLostFoundNotice eduLostFoundNotice)
    {
        eduLostFoundNotice.setCreateBy(getUsername().toString());
        eduLostFoundNotice.setCreateTime(new Date());

        eduLostFoundNotice.setStudentId(getUserId());
        return toAjax(eduLostFoundNoticeService.insertEduLostFoundNotice(eduLostFoundNotice));
    }

    /**
     * 修改寻物启事
     */
    @PreAuthorize("@ss.hasPermi('found:notice:edit')")
    @Log(title = "寻物启事", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduLostFoundNotice eduLostFoundNotice)
    {
        eduLostFoundNotice.setUpdateBy(getUsername().toString());
        eduLostFoundNotice.setUpdateTime(new Date());

        eduLostFoundNotice.setStudentId(getUserId());
        return toAjax(eduLostFoundNoticeService.updateEduLostFoundNotice(eduLostFoundNotice));
    }

    /**
     * 删除寻物启事
     */
    @PreAuthorize("@ss.hasPermi('found:notice:remove')")
    @Log(title = "寻物启事", businessType = BusinessType.DELETE)
	@DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Integer[] noticeIds)
    {

        int total=0;

        for (Integer noticeId: noticeIds){

         EduLostFoundNotice eduLostFoundNotice=eduLostFoundNoticeService.selectEduLostFoundNoticeByNoticeId(noticeId);

            if (eduLostFoundNotice != null) {

                eduLostFoundNotice.setDeleteBy(getUsername().toString());

                eduLostFoundNotice.setDeleteTime(new Date());

                eduLostFoundNotice.setDelFlag(1);

               int a= eduLostFoundNoticeService.updateEduLostFoundNotice(eduLostFoundNotice);

               total=total+a;
            }

        }

        return toAjax(total);
    }


    //下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<EduLostFoundNotice> util = new ExcelUtil<EduLostFoundNotice>(EduLostFoundNotice.class);
        util.importTemplateExcel(response, "用户数据");
    }


    @Log(title = "启事管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('found:notice:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<EduLostFoundNotice> util = new ExcelUtil<EduLostFoundNotice>(EduLostFoundNotice.class);
        List<EduLostFoundNotice> eduLostFoundNoticeList = util.importExcel(file.getInputStream());
        String message = eduLostFoundNoticeService.importUser(eduLostFoundNoticeList, updateSupport);
        return success(message);
    }


    /**
     * 上传
     *
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/uploadMinio")
    @ResponseBody
    public String uploadMinio(@RequestParam(name = "file", required = false) MultipartFile file, HttpServletRequest request) {
        JSONObject res = null;
        try {
            res = minioUtils.uploadFile(file, minioProp.getBucket());
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code", 0);
            res.put("msg", "上传失败");
        }
        return res.toJSONString();
    }

}
